package com.zhimi.tools.mp3extractor;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.net.ssl.HttpsURLConnection;

public class Location {

	class region{
		int id;
		String name;
	}
	private final String USER_AGENT = "Mozilla/5.0";
	
	Map provsMap = new HashMap<Integer, String>();
	Map cityMap = new HashMap<Integer, String>();
	Map cityIDToprovsID = new HashMap<Integer, Integer>();
	
	static int level=0;
	
	static Set cityTask = new HashSet<Integer>();
	static FileWriter writer;
	//static ArrayList<Integer> cityTask = new ArrayList<Integer>();
	
	void LoadRegionInfo() throws Exception{
		cityMap.put(1101, "北京");
		cityMap.put(3101, "上海");
		cityMap.put(1201, "天津");
		cityMap.put(5001, "重庆");
		cityTask.add(1101);
		cityTask.add(1201);
		cityTask.add(3101);
		cityTask.add(5001);
		cityIDToprovsID.put(1101, 1);
		cityIDToprovsID.put(3101, 2);
		cityIDToprovsID.put(1201, 3);
		cityIDToprovsID.put(5001, 4);
		
		BufferedReader br = new BufferedReader(new FileReader("procss city_region.js"));
		String line = br.readLine();
		while (line!= null) {
			String pattern = "(.*)\\svar _city_(\\d+)=\\[(.*)\\].*";

			Pattern r = Pattern.compile(pattern);
			Matcher m = r.matcher(line);

			StringBuilder sb = new StringBuilder();
			if (m.find()) {
				String provs = m.group(1);
				Integer provsid = Integer.parseInt(m.group(2));
				provsMap.put(provsid, provs);
				
				String regions = m.group(3);
				
				String pattern2 = "\"(\\d+)\\:([^\"]*)\"";

				Pattern r2 = Pattern.compile(pattern2);
				Matcher m2 = r2.matcher(regions);

				//System.out.println(regions);
				while (m2.find()) {
					Integer regionID = Integer.parseInt(m2.group(1));
					String region = m2.group(2);
					cityMap.put(regionID, region);
					cityIDToprovsID.put(regionID, provsid);
					
					System.out.println(provsid + " " + provs + " " + regionID + " " + region );
					
					String regionIDstr = m2.group(1);
					if(regionIDstr.length() == 6){
						Integer prefix = Integer.parseInt(regionIDstr.substring(0, 2));
						if(prefix!=11 && prefix!=12&& prefix!=31&& prefix!=50)
							cityTask.add(regionID);
					}
					else if(regionIDstr.length() == 4){
						cityTask.add(regionID);
					}
					else throw new Exception("city id str is not 4 or 6");
					
					
				}
			}
			line = br.readLine();
			
		}
		//PrintMap(provsToID);
	}
	
	void PrintMap(Map myMap ){
		Iterator entries = myMap.entrySet().iterator();
		while (entries.hasNext()) {
		  Entry thisEntry = (Entry) entries.next();
		  Object key = thisEntry.getKey();
		  Object value = thisEntry.getValue();
		  System.out.println(key + " = " +value);
		}
	}

	public static void main(String[] args) throws Exception {

		
		Location http = new Location();
		http.LoadRegionInfo();
		
		Object[] cityarray = cityTask.toArray();
		
		writer=new FileWriter("location.sql");
		for(int i=0;i<cityTask.size();i++)
		{
			int cityid = (int) cityarray[i];
			
			String city = (String) http.cityMap.get(cityid);
			System.out.println("" + " " + city);
			
			level =4;
		//	cityid=1101;
			String url = "http://support.renren.com/juniorschool/" + cityid + ".html";

			http.Parse(http.sendGet(url), city, cityid);
		}
		writer.flush();
		writer.close();
	}

	// HTTP GET request
	private String[] sendGet(String url) throws Exception {

		URL obj = new URL(url);
		HttpURLConnection con = (HttpURLConnection) obj.openConnection();

		// optional default is GET
		con.setRequestMethod("GET");

		// add request header
		con.setRequestProperty("User-Agent", USER_AGENT);

		int responseCode = con.getResponseCode();
		//System.out.println("\nSending 'GET' request to URL : " + url);
		//System.out.println("Response Code : " + responseCode);
		if (responseCode != 200)
			throw new Exception("response is not 200, response=" + responseCode);

		BufferedReader in = new BufferedReader(new InputStreamReader(
				con.getInputStream()));
		String inputLine;
		ArrayList<String> strArray = new ArrayList<String>();
		StringBuffer response = new StringBuffer();

		while ((inputLine = in.readLine()) != null) {
			response.append(inputLine);
			response.append("\n");
			strArray.add(inputLine);
		}
		in.close();

		// print result
		//System.out.println(response.toString());
		String[] a = new String[0];
		return strArray.toArray(a);
		// return strArray.toArray();
	}

	String Int2Char(String str) throws Exception {
		
		
		
		
		String pattern = "&#(\\d+);";

		Pattern r = Pattern.compile(pattern);
		Matcher m = r.matcher(str);

		StringBuilder sb = new StringBuilder();
		while (m.find()) {
			sb.append((char) (Integer.parseInt(m.group(1))));
		}
		
		if(sb.toString().equals("")) {
			pattern = "[a-z\\-]*";

			 r = Pattern.compile(pattern);
			 m = r.matcher(str);
			 if(m.find()){
				 return str;
			 }
			 throw new Exception("error");
		}
			
		return sb.toString();
	}

	void Parse(String[] lines, String city, int cityid) throws Exception {

		HashMap<String, String> region = new HashMap<String,String>();
		
		if (lines[0].startsWith("<ul id=\"schoolCityQuList\" class=")) {
			//System.out.println("find first row;");

			//String pattern = ".*city_qu_(\\d+)\'\\)\">(.*)</a.*";
			String pattern = ".?city_qu_(\\d+)\'\\)\">([0-9&#;]+)</a.?";

			Pattern r = Pattern.compile(pattern);
			Matcher m = r.matcher(lines[0]);

			StringBuilder sb = new StringBuilder();
			while (m.find()) {
				region.put(m.group(1), Int2Char(m.group(2)));
				//System.out.println(m.group(1) + Int2Char(m.group(2)));
			}
		} else if(lines[0].length()==0) {
			
		}
		else
			throw new Exception("first row error");

		boolean startqu = false;
		String qu = "";
		for (int i = 1; i < lines.length; i++) {
			String processLine = lines[i];
			if (startqu == true) {
				if (lines[i].startsWith("<li><a onclick=")) {
					String tmp= lines[i];
					//href="10328322">&#20869;&#27743;&#20845;&#20013;
					String pattern = ".*href=\"\\d+\">(.*)</a>.*";
				
					// 创建 Pattern 对象
					Pattern r = Pattern.compile(pattern);
					Matcher m = r.matcher(lines[i]);
					if (m.find()) {
						String school = m.group(1);
						// decodeURI.
						String school1 = Int2Char(school);
						String provs = (String) provsMap.get(cityIDToprovsID.get(cityid)); 
						

					//	System.out.println( cityIDToprovsID.get(cityid) + "'" +provs + "'," +cityid+"'"
						//		+ city + "',"+qu+"'"+ region.get(qu) +"',\n");
						
				
						//System.out.println("find school " + school1 +" in "+ qu + " "+ region.get(qu) + city);
						continue;
					}
					
					while(!lines[i+1].contains("</a></li>")){
						tmp += lines[i+1];
						i++;
					}
					
					//if(lines[i+1].equals("</a></li></ul>") || lines[i+1].equals("</a></li>") ){
					String compose = tmp+lines[i+1];
					m = r.matcher(compose);
					if (m.find()) {
						String school = m.group(1);
						// decodeURI.
						String school1 = Int2Char(school);
						i++;
						
						
						String provs = (String) provsMap.get(cityIDToprovsID.get(cityid)); 
		
//						writer.write("insert into youqu_school(school_name, country_name, "
//								+ "province_name, city_name, region_name,"
//								+ "school_level,gmt_create) values('" + school1 +"', '中国','" + provs + "', '" 
//								+ city + "','"+ region.get(qu) +"'," + level+", datetime('now', 'localtime'));\n");
//						
//						System.out.println( cityIDToprovsID.get(cityid) + "'" +provs + "'," +cityid+"'"
//								+ city + "',"+qu+"'"+ region.get(qu) +"',\n");
						continue;
					}
					//}

					throw new Exception("failed to find school");

				} else if (lines[i].startsWith("<ul id=\"city_qu_")) {
					startqu = false;
					i--;
					continue;
				} else
					throw new Exception("error3 row error");

			}
			if (startqu == false) {
				if (lines[i].startsWith("<li><a onclick="))
					throw new Exception("error2 row error");

				if (lines[i].startsWith("<ul id=\"city_qu_")) {

					startqu = true;
					String pattern = ".*city_qu_(\\d+).*";

					// 创建 Pattern 对象
					Pattern r = Pattern.compile(pattern);
					Matcher m = r.matcher(lines[i]);
					if (m.find()) {
						qu = m.group(1);
						if(provsMap.get(cityIDToprovsID.get(cityid)) ==null)
							System.out.println("null");
						
						String sql = 
								"insert into youqu_location(province_id, province_name, city_id, city_name, region_id, region_name)"
								+ " values("+ cityIDToprovsID.get(cityid) + ",'" + 
								provsMap.get(cityIDToprovsID.get(cityid)) + "'," +cityid+",'"
								+ city + "',"+qu+",'"+ region.get(qu) + "');\n" ;
						writer.write( sql);
						
						
						//System.out.println("find qu " + qu);
						continue;
					}
					throw new Exception("failed find qu ");

				}
				else if(lines[i].length()==0)
				{
					continue;
				}
				throw new Exception("failed find qu ");

			}
		}

	}

}
